package com.mate.cloud.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


public class LettuceASyncTest {
    public static void main(String[] args) {

        // 1. 构建 Redis 连接信息（明确指定主机、端口、密码）
        RedisURI redisURI = RedisURI.builder()
                .withHost("192.168.110.36")    // 主机地址
                .withPort(6379)                // 端口
                .withPassword("seata@user123")  // 密码（如果没有密码，可省略此句）
                .withDatabase(0)               // 数据库索引（默认0，可选）
                .build();

        RedisClient client = RedisClient.create(redisURI);


        // 线程安全的长连接，连接丢失时会自动重连
        StatefulRedisConnection<String, String> connection = client.connect();
        // 获取异步执行命令api
        RedisAsyncCommands<String, String> commands = connection.async();
        // 获取RedisFuture<T>
        commands.set("gupao:async", "lettuce-async-666");
        RedisFuture<String> future = commands.get("gupao:async");
        try {
            String value = future.get(60, TimeUnit.SECONDS);
            System.out.println("------" + value);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            e.printStackTrace();
        }

    }
}
